<?php
class M_users extends CI_Model {

	public function add_user($userdata)
	{
		if(is_array($userdata)){
			$userdata['password'] = $this->hash_password($userdata['password']);
			$userdata['created'] = NOW;
			$userdata['updated'] = NOW;
			$this->db->insert('users', $userdata) ;
			return $this->db->affected_rows() > 0 ? TRUE : FALSE;
		}
	}
	
	public function verify_user($v){
		
		$this->db->where('username', $v->username);
		$query = $this->db->get('users');
		
		
		if($query->num_rows() >= 1)
		{
			$row = $query->row();
			
		
			if($this->validate_password($row->password, $v->password)){
				if($row->usertype == 'customer' && ($row->verified == 0))
					return 'unverified';
					
				if($row->usertype == 'admin'){
					$userdata = array(
						'admin_id' => $row->id,
						'admin_username' => $row->username,
						'logged_in' => TRUE
					);	
				}
				else if($row->usertype == 'customer'){
					$userdata = array(
						'userid' => $row->id,
						'username' => $row->username,
						'usertype' => $row->usertype,
						'firstname' => $row->firstname,
						'lastname' => $row->lastname,
						'contact' => $row->contact,
						'email' => $row->email,
						'customer_logged_in' => TRUE
					);
				}
				else if($row->usertype == 'stylist'){
					
					$userdata = array(
						'stylist_id' => $row->id,
						's_username' => $row->username,
						's_usertype' => $row->usertype,
						's_firstname' => $row->firstname,
						's_lastname' => $row->lastname,
						's_contact' => $row->contact,
						's_email' => $row->email,
						'stylist_logged_in' => TRUE
					);
				}
		
				$this->session->set_userdata($userdata);
				return 'user_verified';
			}
		}
		else
			return false;
	}
	
	
	public function hash_password($password)
    {
        $salt = bin2hex(mcrypt_create_iv(32, MCRYPT_DEV_URANDOM));
        $hash = hash('sha256', $salt . $password);

        return $salt . $hash;
    }
	
	
	public function validate_password($hashed_password, $password)
    {
        $salt = substr($hashed_password, 0, 64);
        $hash = substr($hashed_password, 64, 64);

        $password_hash = hash('sha256', $salt . $password);
        
        return $password_hash == $hash;
    }
	
	public function verify_password($password,$id)
	{
		$data = $this->db->select('password')->where('id',$id)->get('users');
		foreach($data->result() as $data)
		{
			$h_pass = $data->password;
			break;
		}
		if($this->validate_password($h_pass,$password)){
			return TRUE;	
		}else{
			return FALSE;
		}
	}
	
	public function change_pass($newpassword,$id){

			$newpassword = $this->hash_password($newpassword);
			if($this->db->set('password',$newpassword)->where('id',$id)->update('users')){
				return TRUE;
			}else{
				return FALSE;
			}
	}
	
	public function changepassword($newpassword,$id){

			$newpassword = $this->hash_password($newpassword);
			$this->db->set('password',$newpassword)->where('id',$id)->update('users');

	}
	
	public function getUser($userid)
	{
		$q = $this->db->get_where('users', array('id' => $userid));
		return $q->num_rows() >= 1 ? $q->row() : FALSE;
	}
	
	public function updateUser($userdata, $userid)
	{
		$userdata['updated'] = NOW;
		$this->db->where('id', $userid)->update('users',$userdata);

		return $this->db->affected_rows() > 0 ? TRUE : FALSE;
	}
	
	public function getUsers($usertype){
		$q = $this->db->get_where('users', array('usertype' => $usertype));
		return $q->num_rows() >= 1 ? $q->result() : FALSE;
	}
	
	public function deleteUser($userid)
	{
		$this->db->delete('users', array('id' => $userid));
		return $this->db->affected_rows() > 0 ? TRUE : FALSE;
	}
	
	public function getEmailAddress($email){
		$q = $this->db->query("SELECT * FROM users WHERE email = '$email' AND verified = '0' ");
		return $q->num_rows() >= 1 ? TRUE : FALSE;
	}
	
	public function checkUsernameEmail($username, $email){
		$q = $this->db->query("SELECT * FROM users WHERE email = '$email' || username = '$username' ");
		return $q->num_rows() >= 1 ? TRUE : FALSE;
	}
	
	public function checkEmailAddres($email){
		$q = $this->db->query("SELECT * FROM users WHERE email = '$email' ");
		return $q->num_rows() >= 1 ? $q->row() : FALSE;
	}
	
	public function getIdFromEmail($email){
		$q = $this->db->query("SELECT * FROM users WHERE email = '$email'");
		return $q->num_rows() >= 1 ? $q->row() : FALSE;
	}
	
	public function verifyEmailAddress($email){
		$q = $this->db->query("UPDATE users SET verified = '1' WHERE email = '$email'");
		return $this->db->affected_rows() >= 1 ? TRUE : FALSE;
	}
}
?>